from CritObject import CritObject
class CritPoint(CritObject):
    className = "point"
    def __init__(self, *args, **kwargs):
        super(CritPoint,self).__init__(*args)
        cp = kwargs.pop("point", None)
        if( cp ):
            try:
                import copy
                self.__properties = copy.deepcopy(cp._getPropertiesDict())
            except:
                print "Could not copyrgument object"
            return
        crds = kwargs.pop("crd", None)
        if( crds ):
            ncrds = np.array(crds)
            if( ncrds.ndim != 1 ):
                print "must specify a single coordinate point"
                return # should throw exception
            
            dim = ncrds.shape[0]
            self.setProperty("dim",dim)
            self.setProperty("crd", ncrds)
    def getMinimumDistance(self, p):
        d = self.__properties["crd"] - p
        d = np.sum(d*d,axis=0)
        return d.minimum()
        
    def getDim(self):
        return self.getProperty("dim")
    def getCrd(self):
        return self.getProperty("crd")
    def setCrd(self,crd):
        self.setProperty("crd",crd)
    def _getPropertiesDict(self):
        return self.__properties
    def getSpan(self):
        return (self.getProperty("crd"),self.getProperty("crd"))
    
    def setIntersection(self, lower, upper):
        crds = self.__properties["crd"]
        d = (lower<=crds)*(crds <= upper)
        inds = d.all(axis=0).nonzero()[0]
        try:
            self.setProperty("icrd", crds.take(inds,axis=0))
        except TypeError:
            pass    
    def getDisplay(self, m, disp='xy'):
        """return the intersection points in the display coordinates specified by disp"""
        crd = self.getProperty("icrd")
        if( crd ):
            crd = m*crd
            if( disp == 'xy' ):
                return crd[:,:2]
            elif( disp == 'xz' ):
                return crd[:,0,2]
            else:
                return crd[:,1:]
        else:
            return
